package org.springcloud.wisdom.springcloud_03_freshman.student.controller;

import org.springcloud.wisdom.springcloud_03_freshman.common.bean.ResponseData;
import org.springcloud.wisdom.springcloud_03_freshman.common.controller.BaseController;
import org.springcloud.wisdom.springcloud_03_freshman.student.entity.Card;
import org.springcloud.wisdom.springcloud_03_freshman.student.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("/card")
public class CardController extends BaseController {

    @Autowired
    CardService cardService;

    @RequestMapping("/update")
    public ResponseData<Object> updateCard(
            @RequestParam("sid") String sid,
            @RequestParam("user") String user,
            @RequestParam("location") String location
    ){
        boolean res=false,res2=false;
        Card card=cardService.get(sid);
        if (card == null){
            Card card1=new Card();
            card1.setSid(sid);
            card1.setUser(user);
            card1.setLocation(location);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
            String dataTimeString=sdf.format(new Date());
            card1.setTime(dataTimeString);
            res=cardService.insert(card1);
        }else {
            card.setLocation(location);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh HH:mm:ss");
            String dataTimeString=sdf.format(new Date());
            card.setTime(dataTimeString);
            res2= cardService.update(card);
        }
        return returnObject(res?res:res2);
    }
}
